
package print

import (
	"strconv"
	"dhfshop/models/print"
	"dhfshop/service/gcpool"
    "dhfshop/controllers/base"
	"dhfshop/utils/response"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

type PrintMsgController struct {
	base.BaseController
}

func (this *PrintMsgController) PrintJoin() {
	//curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	//partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	
	curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	if curUserId == 0 {
		this.ServeError("user is not login")
	    return
	}
	storeId := this.GetString("store_id")
	if storeId == "" {
	    this.ServeError("store_id is null")
	    return
	}
	code, err := gcpool.WebsocketHandler(this.Ctx.ResponseWriter, this.Ctx.Request, storeId)
	if code == 0 && err == nil {
		
	} else {
		beego.Error("info:" + response.LangConf.Get(strconv.Itoa(code)).MustString())
		this.ServeError(response.LangConf.Get(strconv.Itoa(code)).MustString())
		return
	}
}

func (this *PrintMsgController) GetPushMsg() {
	//curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	companyId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	//curUserId := this.Ctx.Input.GetData("curUserId").(int)
	offset := 0     //default is 0
	limit := 10    //default is 50
	v := struct {
		Start      string  `form:"start"`  //
		End        string  `form:"end"`
		IsRead     int     `form:"is_read"`
		PageNo     int     `form:"page_no"`
		PageSize   int     `form:"page_size"`
		OrderDesc  int     `form:"order_desc"`
	}{PageSize:10, PageNo:1}     
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Range(v.PageSize,1,1000,"page_size")
	valid.Range(v.PageNo,1,1000,"page_no")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
	}
	v.PageNo = v.PageNo - 1     //为了计算offset
	offset = v.PageNo*v.PageSize
	limit = v.PageSize
	list,cnt,err := print.GetAllPrintQueues(companyId,v.Start,v.End,v.IsRead,v.OrderDesc,offset,limit)
	if err != nil {
	   this.ServeError(err.Error())
	   return
	}
	rsp := struct {
		TotalCount  int64               `json:"total_count"`
		PushMsg     []*print.PrintQueue `json:"push_msgs"`
	}{TotalCount: cnt, PushMsg: list}
	this.Data["json"] = rsp
	this.ServeJSON()
}
